package com.igelato.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.igelato.model.OrderRecharge;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igelato.mapper.OrderRechargeMapper;
import com.igelato.model.User;
import com.igelato.response.AppException;
import com.igelato.response.ErrorCodeMsgEnum;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @TableName: order_recharge
 * @Description: 充值订单
 * @Author: wht
 * @Date: 2024-01-24
 */

@Slf4j
@Service
public class OrderRechargeService extends ServiceImpl<OrderRechargeMapper, OrderRecharge> {
@Autowired
    OrderRechargeMapper orderRechargeMapper;



    @Autowired
    UserMapper userMapper;
    @Autowired
    ConfigService configService;
    @Autowired
    OrderRechargeMapper orderRechargeMapper;

    private User user;
    private Integer user_id;
    private String openid;
    private String ip;
    private BigDecimal pay_money;
    private BigDecimal give_money;


    public void checkParams() {
        if (user_id == null || pay_money == null) {
            throw new AppException(ErrorCodeMsgEnum.PAY_ERROR);
        }
    }

    public void checkUser() {
        user = userMapper.getOneUserById(user_id);
        if (user == null || !user.getOpenid().equals(openid)) {
            throw new AppException(ErrorCodeMsgEnum.PAY_ERROR);
        }
    }


    public void checkRechargeMoney() {
        Config config = configService.getConfig();
        List<Config.RechargeMoney> rechargeMoney = config.getRecharge_money();

        for (Config.RechargeMoney rechargeMoney1 : rechargeMoney) {
            if (rechargeMoney1.getMoney().compareTo(pay_money) == 0) {
                if (rechargeMoney1.getGive().compareTo(give_money) == 0) {
                    return;
                } else {
                    throw new AppException(ErrorCodeMsgEnum.PAY_ERROR);
                }
            }
        }
        throw new AppException(ErrorCodeMsgEnum.PAY_ERROR);
    }


    public JSONObject recharge(String openid, Map body) {
        this.openid = openid;
        this.ip = ip;
        this.user_id = (Integer) body.get("user_id");
        this.pay_money = (BigDecimal) body.get("pay_money");
        this.give_money = (BigDecimal) body.get("give_money");


//        检查
        this.checkParams();
        this.checkUser();
        this.checkRechargeMoney();


//        微信支付
        String trade_no = Utils.getRandomString();
        String wx_pay_verify = Utils.getRandomString();
        JSONObject respdata = new JSONObject();
//        JSONObject respdata = Utils.WxPay("充值", openid, trade_no, wx_pay_verify, new BigDecimal(0.01), "/2c/recharge_callback");
//        JSONObject respdata = Utils.WxPay("充值", openid, tradeNo, ip, pay_money, "/2c/paycallback_recharge");

        // 新增订单
        OrderRecharge orderRecharge = new OrderRecharge();
        orderRecharge.setTrade_no(trade_no);
        orderRecharge.setWx_pay_verify(wx_pay_verify);
        orderRecharge.setStatus(1);
        orderRecharge.setPay_money(pay_money);
        orderRecharge.setRecharge_money(pay_money.add(give_money));
        orderRecharge.setWx_pre_pay_info(respdata.toJSONString());
        String tradeNo = Utils.getRandomString();
        orderRecharge.setTrade_no(tradeNo);
        orderRechargeMapper.addOneOrderRecharge(orderRecharge);

        return (JSONObject) respdata.get("payment");
    }


    public void rechargeCallback(Map mapBody) {
        OrderRecharge orderRecharge = new OrderRecharge();
        orderRecharge.setTrade_no((String) mapBody.get("outTradeNo"));
        orderRecharge.setWx_pay_verify((String) mapBody.get("attach"));
        orderRecharge.setStatus(2);
        orderRecharge.setWx_pay_callback_info(JSON.toJSONString(mapBody));
        orderRechargeMapper.payWxSuccess(orderRecharge);
    }


    //    更新数据库 ---------------------------------------
    public List<OrderRecharge> getOrderRechargeList(Integer user_id) {
        List<OrderRecharge> orderRechargeList = orderRechargeMapper.getOrderRechargeList(user_id);
        return orderRechargeList;
    }

}
